package com.test.prolenders.dialog_prolenders;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.test.prolenders.R;
import com.test.prolenders.bean_prolenders.config_prolenders.AppShowCustomize_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.BankAccount_Fr_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class DeleteAccountDialog_Fr_prolenders extends Dialog {

    private final FragmentActivity activity;

    private final BankAccount_Fr_prolenders account;

    private final String page;

    private final Runnable dltActRespRunnable;

    public DeleteAccountDialog_Fr_prolenders(@NonNull FragmentActivity activity, BankAccount_Fr_prolenders account, String page, Runnable dltActRespRunnable) {
        super(activity);
        this.activity = activity;
        this.account = account;
        this.page = page;
        this.dltActRespRunnable = dltActRespRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete_account_fr_prolenders);

        getWindow().setBackgroundDrawableResource(R.color.transparent_prolenders);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        TextView accountmobileTv  = findViewById(R.id.ddaf_tv_accountmobile);
        ImageView accountlogoIv = findViewById(R.id.ddaf_iv_logo);
        TextView accountchannelTv = findViewById(R.id.ddaf_tv_accountchannel);

        //取消
        findViewById(R.id.ddaf_btn_cancel).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"_DELETE_CANCEL_CLICK",account.getWalletMobile());
            dismiss();
        });

        //提交
        findViewById(R.id.ddaf_btn_submit).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"_DELETE_CONFIRM_CLICK",account.getWalletMobile());
            HttpUtil_prolenders.getInstance().deleteAccount_Fr(activity,account.getId(),() -> {
                if (dltActRespRunnable!=null) dltActRespRunnable.run();
            });
            dismiss();
        });

        if (account != null){

            String channelDesc = account.getWalletDesc();

            accountmobileTv.setText(account.getWalletMobile());
            accountchannelTv.setText(channelDesc);

            //加载图标
            if (AppShowCustomize_prolenders.getInstance().getChannelLogoByDesc(channelDesc) == null){
                new Thread(() -> {
                    try {
                        if (TextUtils.isEmpty(account.getWalletLogo())) return;
                        if (!AppShowCustomize_prolenders.getInstance().hasAccountChannel()) return;
                        URL url = new URL(account.getWalletLogo());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            AppShowCustomize_prolenders.getInstance().setChannelLogoByDesc(channelDesc,bitmap);
                            new Handler(Looper.getMainLooper()).post(() -> accountlogoIv.setImageBitmap(bitmap));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }else accountlogoIv.setImageBitmap(AppShowCustomize_prolenders.getInstance().getChannelLogoByDesc(channelDesc));

        }

    }

}
